'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.Linq
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized

Public Class ContactViewModel
    Inherits System.Windows.Threading.DispatcherObject
    Implements INotifyPropertyChanged

    Private _Model As IcqInterface.Interfaces.IContact
    Private _GroupVm As GroupViewModel

    Private _ContactImageCreated As Boolean
    Private _ContactImage As ImageSource

    Public Sub New(ByVal model As IcqInterface.Interfaces.IContact)
        _Model = model

        AddHandler _Model.PropertyChanged, AddressOf HandlePropertyChanged

        AddHandler _Model.IconHashReceived, AddressOf OnIconHashReceived
        AddHandler _Model.IconDataReceived, AddressOf OnIconDataReceived
    End Sub

    Public ReadOnly Property Model() As IcqInterface.Interfaces.IContact
        Get
            Return _Model
        End Get
    End Property

    Public ReadOnly Property Attributes() As System.Collections.Hashtable
        Get
            Return _Model.Attributes
        End Get
    End Property

    Public ReadOnly Property Identifier() As String
        Get
            Return _Model.Identifier
        End Get
    End Property

    Public ReadOnly Property Name() As String
        Get
            Return _Model.Name
        End Get
    End Property

    Public ReadOnly Property Group() As GroupViewModel
        Get
            If _Model.Group Is Nothing Then Return Nothing
            If _GroupVm Is Nothing Then _GroupVm = GroupViewModelCache.GetViewModel(_Model.Group)

            Return _GroupVm
        End Get
    End Property

    Public ReadOnly Property MemberSince() As Date
        Get
            Return Model.MemberSince
        End Get
    End Property

    Public ReadOnly Property SignOnTime() As Date
        Get
            Return Model.SignOnTime
        End Get
    End Property

    Public ReadOnly Property FirstName() As String
        Get
            Return Model.FirstName
        End Get
    End Property

    Public ReadOnly Property LastName() As String
        Get
            Return Model.LastName
        End Get
    End Property

    Public ReadOnly Property EmailAddress() As String
        Get
            Return Model.EmailAddress
        End Get
    End Property

    Public ReadOnly Property Gender() As IcqInterface.Interfaces.ContactGender
        Get
            Return Model.Gender
        End Get
    End Property

    Public ReadOnly Property AuthorizationRequired() As Boolean
        Get
            Return Model.AuthorizationRequired
        End Get
    End Property


    Public ReadOnly Property Status() As IcqInterface.Interfaces.IStatusCode
        Get
            Return _Model.Status
        End Get
    End Property

    Public ReadOnly Property StatusFlag() As Integer
        Get
            If Status Is IcqInterface.IcqStatusCodes.Online Then
                Return 0
            ElseIf Status Is IcqInterface.IcqStatusCodes.Offline Then
                Return 2
            Else
                Return 1
            End If
        End Get
    End Property

    Public ReadOnly Property StatusBrush() As Brush
        Get
            If Status Is IcqInterface.IcqStatusCodes.Online Then
                Return DirectCast(Application.Current.Resources("vbrOnline"), Brush)
            ElseIf Status Is IcqInterface.IcqStatusCodes.Offline Then
                Return DirectCast(Application.Current.Resources("vbrOffline"), Brush)
            Else
                Return DirectCast(Application.Current.Resources("vbrAway"), Brush)
            End If
        End Get
    End Property

    Protected Sub HandlePropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        OnPropertyChanged(e)

        If e.PropertyName = "Status" Then
            OnPropertyChanged(New PropertyChangedEventArgs("StatusFlag"))
            OnPropertyChanged(New PropertyChangedEventArgs("StatusBrush"))
        End If
    End Sub

    Protected Sub OnPropertyChanged(ByVal e As System.ComponentModel.PropertyChangedEventArgs)
        If Group IsNot Nothing Then Group.OnContactPropertyChanged(Me, e)

        RaiseEvent PropertyChanged(Me, e)
    End Sub

    Private Sub CreateContactImage()
        _ContactImageCreated = True

        If Model.IconData IsNot Nothing AndAlso Model.IconData.Count > 0 Then
            Dim decoder As System.Windows.Media.Imaging.BitmapDecoder
            Dim ms As New System.IO.MemoryStream(Model.IconData.ToArray)

            ms = New System.IO.MemoryStream(Model.IconData.ToArray)
            decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.DelayCreation, BitmapCacheOption.Default)

            If decoder IsNot Nothing Then _ContactImage = decoder.Frames(0)
        End If
    End Sub

    Public ReadOnly Property ContactImage() As ImageSource
        Get
            If Not _ContactImageCreated Then CreateContactImage()

            Return _ContactImage
        End Get
    End Property

    Public ReadOnly Property ContactImageVisibility() As Visibility
        Get
            If _ContactImage IsNot Nothing Then
                Return Visibility.Visible
            Else
                Return Visibility.Collapsed
            End If
        End Get
    End Property

    Private Sub OnIconDataReceived(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, New Action(AddressOf CreateContactImage))

            OnPropertyChanged("ContactImage")
            OnPropertyChanged("ContactImageVisibility")
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnIconHashReceived(ByVal sender As Object, ByVal e As EventArgs)
        Dim svAvatar As IcqInterface.Interfaces.IIconService
        Try
            svAvatar = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IIconService)()
            svAvatar.RequestContactIcon(Model)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Protected Sub OnPropertyChanged(ByVal propertyName As String)
        OnPropertyChanged(New ComponentModel.PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class